package com.bilibili.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bilibili.dubbo.api.db.SettingApi;
import com.bilibili.dubbo.api.mapper.SettingsMapper;
import com.bilibili.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.print.attribute.standard.Finishings;

/**
 * @author HuangRongFu
 * 2021/11/17 星期三9:27 下午
 */

@DubboService
public class SettingApiImpl implements SettingApi {

    @Autowired
    private SettingsMapper settingsMapper;


    @Override
    public Settings findByUserId(Long userId) {

        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);

        return settingsMapper.selectOne(wrapper);
    }

    /**
     * 通用设置保存
     *@param: settings
     */
    @Override
    public void saveSettings(Settings pojo) {

        Settings settingsInDb = findByUserId(pojo.getUserId());

        //进行非空判断
        if (null != settingsInDb){

            //存在则更新
            pojo.setId(settingsInDb.getId());
            settingsMapper.updateById(pojo);
        }else {

            //不存在就新增
            settingsMapper.insert(pojo);
        }
    }
}
